"""
* page就是网页的页面层，object就是封装的LoginPage 类:每一个页面一个类，包含业务逻辑和测试对象
    * 页面元素元素定位: 定义为类的属性
    * 页面的操作行为 ： 定义为类的实例方法
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class LoginPage:
    # 用户名输入框
    loc_username = (By.XPATH, '//input[@placeholder="请输入手机号/用户名"]')
    # 密码输入框
    loc_password = (By.XPATH, '//input[@placeholder="请输入密码"]')
    # 登录按钮
    loc_login_but = (By.CLASS_NAME, 'login-button')

    # 操作和行为 行为为实例方法
    def wait_element_clickable(self,driver,locator):
        web_element = WebDriverWait(driver, 8, 0.5).until(EC.element_to_be_clickable(locator))
        return web_element

    def wait_element_visible(self,driver,locator):
        web_element = WebDriverWait(driver, 8, 0.5).until(EC.visibility_of_element_located(locator))
        return web_element

    def wait_element_presence(self,driver,locator):
        web_element = WebDriverWait(driver, 8, 0.5).until(EC.presence_of_element_located(locator))
        return web_element

    # 登录操作 --行为 实例方法 --参数化
    def login(self,driver,uname,pwd):
        # 1、输入账号
        self.wait_element_visible(driver,self.loc_username).send_keys(uname)
        # 2、输入密码
        self.wait_element_visible(driver,self.loc_password).send_keys(pwd)
        # 3、点击登录按钮
        self.wait_element_clickable(driver,self.loc_login_but).click()